package com.example.hadoop.mapreduce.comonfriend;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Brian
 * @Date: 2020/04/26/8:24
 * @Description: A:B,C,D,F,E,O
 * B:A,C,E,K
 * C:F,A,D,I
 * D:A,E,F,L
 * E:B,C,D,M,L
 * F:A,B,C,D,E,O,M
 * G:A,C,D,E,F
 * H:A,C,D,E,O
 * I:A,O
 * J:B,O
 * K:A,C,D
 * L:D,E,F
 * M:E,F,G
 * O:A,H,I,J
 * <p>
 * 求出哪些人两两之间有共同好友，及他俩的共同好友都是谁
 * 比如:
 * a-b :  c ,e
 */
public class GroupFriend {
    static class GroupFriendMapper extends Mapper<LongWritable, Text, Text, Text> {
        Text k = new Text();
        Text v = new Text();

        /**
         * 输出：冒号前面的字母作为value， 冒号后面字母分别作为key
         *      <friend1,person1>
         *      <friend2,person1>
         *      <friend3,person1>
         *
         * @param key
         * @param value
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] splits = line.split(":");
            v.set(splits[0]);
            for (String friend : splits[1].split(",")) {
                k.set(friend);
                context.write(k, v);
            }
        }
    }

    static class GroupFriendCombiner extends Reducer<Text, Text, Text, Text> {
        Text k = new Text();
        Text v = new Text();

        /**
         * <A,B><A,C><A,D>....
         * <B,A><B,E><B,J>....
         * 输出：
         * <B-C,A><B-D,A><C-D,A>....
         * <A-E,B><A-J,B><E-J,B>....
         *
         * @param key
         * @param values
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            String comFriend = key.toString();
            v.set(comFriend);
            List<String> keys = new ArrayList<>();
            for (Text text : values) {
                keys.add(text.toString());
            }
            // list为具有共同好友A的  [B,C,D]
            Collections.sort(keys);
            for (int i = 0; i < keys.size() - 1; i++) {
                String first = keys.get(i);
                for (int j = i+1; j < keys.size(); j++) {
                    String second = keys.get(j);
                    k.set(first + "-" + second);
                    context.write(k, v);
                }
            }

        }
    }

    static class GroupFriendReducer extends Reducer<Text, Text, Text, Text> {
        Text v = new Text();

        /**
         * <B,A><B,E><B,F>......
         * <C,A><C,B><C,E>......
         * <friend1,person1><friend1,person2><friend1,person3>
         * <friend2,person1><friend2,person2><friend2,person3>
         * <friend3,person1><friend3,person2><friend3,person3>
         *
         * @param key
         * @param values
         * @param context
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            List<String> commonFriends = new ArrayList<>();
            for (Text text : values) {
                commonFriends.add(text.toString());
            }
            StringBuffer sb = new StringBuffer();
            for (String friend : commonFriends) {
                sb.append(friend).append(",");
            }
            if (sb.toString().length() > 0) {
                String friend = sb.deleteCharAt(sb.length() -1).toString();
                v.set(friend);
                context.write(key, v);
            }
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        if (args == null || args.length < 2) {
            args[0] = "/wordcount/flowinput";
            args[1] = "/wordcount/flowoutput";
        }
        //加载默认配置
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        //当程序运行时，通过类加载器获取类的所在路径
        job.setJarByClass(GroupFriend.class);
        //配置mapper和reducer类
        job.setMapperClass(GroupFriendMapper.class);
        job.setCombinerClass(GroupFriendCombiner.class);
//        job.setReducerClass(GroupFriendReducer.class);

        //配置map的输出类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);

        //配置最终输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputKeyClass(Text.class);

        Path outputPath = new Path(args[1]);
        FileSystem fileSystem = FileSystem.get(new Configuration());
        if (fileSystem.exists(outputPath)) {
            fileSystem.delete(outputPath, true);
        }
        //设置数据源路径和输入路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, outputPath);

        //将Job提交给Yarn
        boolean successfully = job.waitForCompletion(true);
        System.out.println("Step one is successfully? --> " + successfully);

        if (successfully) {
            job = Job.getInstance(conf);
            job.setJarByClass(GroupFriendSepTwo.class);
            job.setMapperClass(GroupFriendSepTwo.GroupFriendSepTwoMapper.class);
            job.setReducerClass(GroupFriendSepTwo.GroupFriendSepTwoReducer.class);

            job.setOutputKeyClass(Text.class);
            job.setOutputKeyClass(Text.class);

            Path stepTwoOutputPath = new Path("F:/hadoop/test/friends/output1");
            fileSystem = FileSystem.get(new Configuration());
            if (fileSystem.exists(stepTwoOutputPath)) {
                fileSystem.delete(stepTwoOutputPath, true);
            }
            FileInputFormat.setInputPaths(job, outputPath);
            FileOutputFormat.setOutputPath(job, stepTwoOutputPath);
            successfully = job.waitForCompletion(true);
        }

        System.out.println("Successfully? --> " + successfully);
        System.exit(successfully ? 0 : 1);
    }
}
